package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class checkGroupController {
@Autowired
private CheckGroupService checkGroupService;

    /**
     * 查找所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(checkGroups!=null?true:false,checkGroups!=null? MessageConstant.QUERY_CHECKGROUP_SUCCESS:MessageConstant.QUERY_CHECKGROUP_FAIL,checkGroups);
    }


    /**
     * 跟据id查找检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(checkGroup!=null?true:false,checkGroup!=null? MessageConstant.QUERY_CHECKGROUP_SUCCESS:MessageConstant.QUERY_CHECKGROUP_FAIL,checkGroup);
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean temp = checkGroupService.deleteById(id);
        return new Result(temp,temp?MessageConstant.DELETE_CHECKGROUP_SUCCESS:MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    /**
     * 根据套餐id查询关联的检查项id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        List<Integer> list = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(list!=null?true:false,list!=null?MessageConstant.QUERY_CHECKGROUP_SUCCESS:MessageConstant.QUERY_CHECKGROUP_FAIL,list);
    }

    /**
     * 编辑
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,List<Integer> checkitemIds){
        boolean temp = checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(temp,temp?MessageConstant.EDIT_CHECKGROUP_SUCCESS:MessageConstant.EDIT_CHECKGROUP_FAIL);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return  checkGroupService.findPage(queryPageBean);
    }

    /**
     * 新增检查组并加入对于的检查项
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,List<Integer> checkitemIds){
        boolean temp = checkGroupService.add(checkGroup,checkitemIds);
        return new Result(temp,temp?MessageConstant.ADD_CHECKGROUP_SUCCESS:MessageConstant.ADD_CHECKGROUP_FAIL);
    }
}
